﻿namespace PageTransition.View
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using WP7Contrib.Services.Navigation;
    using WP7Contrib.View.Transitions.Animation;
    using WP7Contrib.View.Transitions.Extensions;

    public partial class MainPage : AnimatedBasePage
    {
        private DialogService dialogService;

        private readonly INavigationService navigationService; 

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            AnimationContext = this.LayoutRoot;

            this.navigationService = new ApplicationFrameNavigationService(((App)Application.Current).RootFrame);
        }

        protected override bool IsPopupOpen()
        {
            return this.dialogService != null && this.dialogService.IsOpen;
        }

        protected override AnimatorHelperBase GetAnimation(AnimationType animationType, Uri toOrFrom)
        {
            // You could factor this into an intermediate base page to have some other defaults
            // such as always continuuming to a pivot page or rules based on the page, direction and where you are going to/coming from

            if (toOrFrom != null)
            {
                if (toOrFrom.OriginalString.Contains("TurnstileFeather.xaml"))
                {
                    if (animationType == AnimationType.NavigateForwardOut)
                    {
                        return new TurnstileFeatherForwardOutAnimator { ListBox = this.AnimationList, RootElement = this };
                    }

                    return new TurnstileFeatherBackwardInAnimator { ListBox = this.AnimationList, RootElement = this };
                }

                if (toOrFrom.OriginalString.Contains("Continuum.xaml"))
                {
                    var element = this.AnimationList.ItemContainerGenerator.ContainerFromIndex(this.AnimationList.SelectedIndex) ?? this.AnimationList;
                    if (element is FrameworkElement)
                    {
                        return this.GetContinuumAnimation((FrameworkElement)element, animationType);
                    }
                }
                
                if (toOrFrom.OriginalString.Contains("Slide.xaml"))
                {
                    //want to keep the page as is so return null
                    return null;
                }
                
                if (toOrFrom.OriginalString.Contains("Rotate180Clockwise.xaml"))
                {
                    if (animationType == AnimationType.NavigateForwardOut)
                    {
                        return new RotateOut180ClockwiseAnimator { RootElement = this };
                    }
                    
                    return new RotateIn180ClockwiseAnimator { RootElement = this };
                }

                if (toOrFrom.OriginalString.Contains("Rotate180CounterClockwise.xaml"))
                {
                    if (animationType == AnimationType.NavigateForwardOut)
                    {
                        return new RotateOut180CounterClockwiseAnimator { RootElement = this };
                    }

                    return new RotateIn180CounterClockwiseAnimator { RootElement = this };
                }

                if (toOrFrom.OriginalString.Contains("Rotate90Clockwise.xaml"))
                {
                    if (animationType == AnimationType.NavigateForwardOut)
                    {
                        return new RotateOut90ClockwiseAnimator { RootElement = this };
                    }

                    return new RotateIn90ClockwiseAnimator { RootElement = this };
                }

                if (toOrFrom.OriginalString.Contains("Rotate90CounterClockwise.xaml"))
                {
                    if (animationType == AnimationType.NavigateForwardOut)
                    {
                        return new RotateOut90CounterClockwiseAnimator { RootElement = this };
                    }

                    return new RotateIn90CounterClockwiseAnimator { RootElement = this };
                }
            }

            return base.GetAnimation(animationType, toOrFrom);
        }

        protected override void AnimationsComplete(AnimationType animationType)
        {
            switch (animationType)
            {
                case AnimationType.NavigateForwardIn:
                    //Add code to set data context and bind data
                    //you really only need to do that on forward in. on backward in everything
                    //will be there that existed on forward out
                    break;

                case AnimationType.NavigateBackwardIn:
                    //reset list so you can select the same element again
                    AnimationList.SelectedIndex = -1;
                    break;
            }

            base.AnimationsComplete(animationType);
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
            {
                switch (AnimationList.SelectedIndex)
                {
                    case 0:
                        this.navigationService.Navigate(new Uri("/View/Turnstile.xaml", UriKind.Relative));
                        break;

                    case 1:
                        this.navigationService.Navigate(new Uri("/View/TurnstileFeather.xaml", UriKind.Relative));
                        break;

                    case 2:
                        this.navigationService.Navigate(new Uri("/View/Continuum.xaml", UriKind.Relative));
                        break;

                    case 3:
                        //slide right
                        this.navigationService.Navigate(new Uri("/View/SlideRight.xaml", UriKind.Relative));
                        break;

                    case 4:
                        //slide left
                        this.navigationService.Navigate(new Uri("/View/SlideLeft.xaml", UriKind.Relative));
                        break;

                    case 5:
                        //slide right
                        this.navigationService.Navigate(new Uri("/View/Slide.xaml", UriKind.Relative));
                        break;

                    case 6:
                        //slide (popup)
                        this.dialogService = new DialogService
                                                 {
                                                     Child = new SlideControl(),
                                                     AnimationType = DialogService.AnimationTypes.Slide
                                                 };
                        this.dialogService.Closed += OnDialogServiceClosed;
                        this.dialogService.Show();
                        break;


                    case 7:
                        this.dialogService = new DialogService
                                                 {
                                                     Child = new SlideControl(),
                                                     AnimationType = DialogService.AnimationTypes.Swivel
                                                 };
                        this.dialogService.Closed += OnDialogServiceClosed;
                        this.dialogService.Show();
                        break;

                    case 8:
                        //rotate 90 clockwise
                        this.navigationService.Navigate(new Uri("/View/Rotate90Clockwise.xaml", UriKind.Relative));
                        break;

                    case 9:
                        //rotate 90  counter clockwise
                        this.navigationService.Navigate(new Uri("/View/Rotate90CounterClockwise.xaml", UriKind.Relative));
                        break;

                    case 10:
                        //rotate 180 clockwise
                        this.navigationService.Navigate(new Uri("/View/Rotate180Clockwise.xaml", UriKind.Relative));
                        break;

                    case 11:
                        //rotate 180 counter clockwise
                        this.navigationService.Navigate(new Uri("/View/Rotate180CounterClockwise.xaml", UriKind.Relative));
                        break;
                }
            }
        }

        private void OnDialogServiceClosed(object sender, EventArgs e)
        {
            this.dialogService.Closed -= OnDialogServiceClosed;
            AnimationList.SelectedIndex = -1;
        }
    }
}